package cn.hp.controller;

import cn.hp.entity.Announcement;
import cn.hp.service.AnnouncementService;
import cn.hp.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;
    @RequestMapping("/page")
    public Result page(Announcement announcement,@RequestParam(value = "page", defaultValue = "1") int page,@RequestParam(value = "limit", defaultValue = "10") int limit) {
        PageInfo<Announcement> pageinfo = announcementService.findAnnouncement(announcement, page, limit);
        return Result.success(pageinfo);
    }

    @PostMapping
    public Result add(@RequestBody Announcement announcement){
        int i = announcementService.insertAnnouncement(announcement);
//        if (i > 0) {
//         return Result.success("新增成功");
//        } else {
//        return Result.error("新增失败");
//        }
        return i > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        int i = announcementService.deleteAnnouncementById(id);
        return i > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping
    public Result update(@RequestBody Announcement announcement){
        int i = announcementService.updateAnnouncement(announcement);
        return i > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping
    public Result find(Announcement announcement){
        List<Announcement> list = announcementService.findAnnouncement(announcement);
        return Result.success(list);
    }

}
